//
//  DetailMainView.m
//  哈哈
//
//  Created by 奇艺果 on 2017/12/25.
//  Copyright © 2017年 奇艺果. All rights reserved.
//

#import "DetailMainView.h"
#import "QYGDollDetailView.h"
#import "QYGDollRecordView.h"

#define titleViewH  ScaleW(50)

@interface DetailMainView()<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UILabel *titleLineLabel;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, weak)   UIButton *selectorButton;
@property (nonatomic, strong)   UIView *middleLine;
@property (nonatomic, strong)   QYGDollDetailView *dollDetailView;
@property (nonatomic, strong)   QYGDollRecordView *dollRecordView;
@end



@implementation DetailMainView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.titleArray = @[@"娃娃详情",@"抓取记录"];
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)setDetail_img:(NSURL *)detail_img{
    _detail_img = detail_img;
    self.dollDetailView.detail_img = detail_img;
}
#pragma mark target

- (void)titleBtnClick:(UIButton *)btn
{
    //1. 切换按钮
    [self titleChangeButton:btn];
    //2. 添加对应的控制器view到滚动区
    self.scrollView.contentOffset = CGPointMake(btn.tag * self.scrollView.width, 0);
    
}

#pragma mark -UIScrollViewDelegate
/**
 *  滚动完成的时候
 *
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / (int)scrollView.width;
    //2. 选中按钮
    [self titleChangeButton:self.buttons[i]];
}

#pragma mark -内部方法
/**
 *  切换按钮
 *
 */
- (void)titleChangeButton:(UIButton *)btn
{
    self.selectorButton.selected = NO;
    self.selectorButton.titleLabel.font = ScaleFont(15);
    self.selectorButton.transform = CGAffineTransformIdentity;
    btn.selected = YES;
    self.selectorButton = btn;
    NSString *content = btn.titleLabel.text;
    UIFont *font = btn.titleLabel.font;
    CGSize size = CGSizeMake(MAXFLOAT, 30.0f);
    CGSize buttonSize = [content boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName:font} context:nil].size;
    self.titleLineLabel.width = buttonSize.width;
    self.titleLineLabel.centerX = btn.centerX;
    
    if(btn.tag == 1 && !self.dollRecordView){
        
        self.dollRecordView = [[QYGDollRecordView alloc] init];
        self.dollRecordView.deviceId = self.deviceId;
        [self.scrollView addSubview:self.dollRecordView];
        self.dollRecordView.frame = CGRectMake(self.scrollView.width + ScaleW(15), 0, self.scrollView.width - 2 * ScaleW(15), self.scrollView.height - ScaleW(15));
    }
    
}

#pragma mark setUI
- (void)setupUI{
    
    self.backgroundColor = [UIColor colorWithHex:@"ffb52b"];
    self.layer.cornerRadius = 10;
    
    [self addSubview:self.titleView];
    self.titleView.x = ScaleW(15);
    [self setupBtns];
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.dollDetailView];
    self.dollDetailView.frame = CGRectMake(ScaleW(15), 0, self.scrollView.width - 2 * ScaleW(15), self.scrollView.height - ScaleW(15));
    self.scrollView.contentSize = CGSizeMake(self.titleArray.count *self.scrollView.width, 0);
    
}
- (void)setupBtns{
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.titleView.width/self.titleArray.count;
    CGFloat h = self.titleView.height;
    for (int i = 0; i < self.buttons.count; i++) {
        
        x = i *w;
        UIButton *btn = self.buttons[i];
        btn.frame = CGRectMake(x, y, w, h);
        
        if (i == 0)
        {
            [self titleBtnClick:btn];
        }
    }
}
- (UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, titleViewH, self.width, self.height -titleViewH)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (UIView *)titleView
{
    if(!_titleView)
    {
        _titleView = [[UIView alloc] init];
//        _titleView.backgroundColor = [UIColor redColor];
        [_titleView addSubview:self.titleLineLabel];
        _titleView.size = CGSizeMake(self.width - ScaleW(30),titleViewH);
        self.titleLineLabel.y = self.titleView.height *0.8;
        
        for (int i = 0; i <self.titleArray.count; i++) {
            UIButton *btn = [[UIButton alloc] init];
            btn.tag = i;
            [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
            btn.titleLabel.font = ScaleFont(15);
            [btn setTitleColor:[UIColor colorWithHex:@"ffd364"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHex:@"ffffff"] forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.titleView addSubview:btn];
            [self.buttons addObject:btn];
            [_titleView addSubview:self.middleLine];
            self.middleLine.y = (titleViewH - self.middleLine.height) * 0.5;
            self.middleLine.x = (_titleView.width - self.middleLine.width) * 0.5;
        }
    }
    return _titleView;
}

- (UILabel *)titleLineLabel
{
    if(!_titleLineLabel)
    {
        _titleLineLabel = [[UILabel alloc] init];
        _titleLineLabel.backgroundColor = [UIColor whiteColor];
        _titleLineLabel.height = 2;
    }
    return _titleLineLabel;
}

- (NSMutableArray *)buttons
{
    if (_buttons == nil)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (QYGDollDetailView *)dollDetailView{
    if(!_dollDetailView){
        _dollDetailView = [[QYGDollDetailView alloc] init];
        
    }
    return _dollDetailView;
}
- (UIView *)middleLine{
    if(!_middleLine){
        _middleLine = [UIView new];
        _middleLine.width = 1;
        _middleLine.height = ScaleW(13);
        _middleLine.backgroundColor = [UIColor colorWithHex:@"ffda64"];
    }
    return _middleLine;
}
//- (QYGDollRecordView *)dollRecordView{
//    if(!_dollRecordView){
//        _dollRecordView = [[QYGDollRecordView alloc] init];
//    }
//    return _dollRecordView;
//}
@end
